﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vmago.Models
{
    public class RepositoryShopVoucher:IShopVoucher
    {

        #region IShopVoucher Members

        private ShopVoucherDataContext ds;

        public RepositoryShopVoucher()
        {
            ds = new ShopVoucherDataContext();
        }

        public IEnumerable<ShopVoucher> getShopVoucher(int shopID)
        {
            IEnumerable<ShopVoucher> c = null;
            try
            {
                c = from p in ds.ShopVouchers
                    where p.shopID.Equals(shopID)
                    select p;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        #endregion

        #region IShopVoucher Members


        public int createShopVoucher(int shopID, string voucharName,string voucherFilename,string description,DateTime startDate,DateTime endDate,string conditions,DateTime publishDate)
        {
            int c = 0;
            try
            {
                ds.updateShopVoucher("INS", 0, shopID, voucharName, voucherFilename, description, startDate, endDate, conditions, publishDate);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int updateShopVoucher(int voucherID, int shopID, string voucharName, string voucherFilename, string description, DateTime startDate, DateTime endDate, string conditions, DateTime publishDate)
        {
            int c = 0;
            try
            {
                ds.updateShopVoucher("UPD", voucherID, shopID, voucharName, voucherFilename, description, startDate, endDate, conditions, publishDate);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        public int deleteShopVoucher(int voucherID)
        {
            int c = 0;
            DateTime currentTime = System.DateTime.Now;
            try
            {
                ds.updateShopVoucher("DEL", voucherID, 0, "", "", "", currentTime, currentTime, "", currentTime);
                c = 1;
            }
            catch (Exception ex)
            {
                ds.Dispose();
                Console.WriteLine(ex.ToString());
                throw;
            }
            return c;
        }

        #endregion
    }
}
